/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufpr.tads.Rent.view;

import br.ufpr.tads.Rent.controller.Automovel;
import br.ufpr.tads.Rent.controller.EnumClass.*;
import br.ufpr.tads.Rent.model.VeiculoDTO;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author NI LINDA
 */

public class ModeloTabelaVeiculo2 extends AbstractTableModel{
    private String[] colunas=new String[]{"Placa","Marca", "Modelo", "Ano","Preco de Venda"};

    private List<VeiculoDTO> lista=new ArrayList();
    
    @Override
    public int getRowCount() {
        return this.lista.size();
    }

    @Override
    public int getColumnCount() {
        return this.colunas.length;
    }

    @Override
    public String getColumnName(int index) {
        return this.colunas[index];
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
        //if(column==0)
            //return false;
        //return true;
    }

    
    public void ModeloTabelaVeiculos(List<VeiculoDTO> lista){
        this.lista=lista;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        VeiculoDTO vei = lista.get(rowIndex);
        try {
            Automovel aut = new Automovel();
       
        switch (columnIndex) {
            case 0: return vei.getPlaca();
            case 1: return vei.getMarca();
            case 2: if(vei.getTipo().getKey()==0){
                        return ModeloAutomovel.values()[vei.getModelo()];
                    }else if(vei.getTipo().getKey()==1){
                        return modelomotocicleta.values()[vei.getModelo()];
                    }else{
                        return modelovan.values()[vei.getModelo()].getNome();
                    }
            case 3: return vei.getAno();
            case 4:  return aut.getValorParaVenda(vei.getId());
            default : return null;
        }
         } catch (IOException | ClassNotFoundException | SQLException ex) {
            Logger.getLogger(ModeloTabelaVeiculo2.class.getName()).log(Level.SEVERE, null, ex);
         }
        return 0;
    }
       
        
    


    public void setListaVeiculos(List<VeiculoDTO> lista) {
        this.lista=lista;
        this.fireTableDataChanged();
    }

    public void limpaTabela() {
        int indice = lista.size()-1;
        if(indice<0)
            indice=0;
        this.lista = new ArrayList();
        this.fireTableRowsDeleted(0,indice);//update JTable
    }

    public VeiculoDTO getVeiculo(int linha){
        return lista.get(linha);
    }
    
}
